﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
using Web.Framework;
using Service;
using System.Web.Security;
using Entity.Admin;

namespace Web.Areas.Admin.Controllers
{
    public partial class AccountController : BaseController
    {
        private UserService userService;

        public AccountController()
        {
            userService = new UserService(db);
        }

        public virtual ActionResult LogOn()
        {
            return View();

        }
        [HttpPost]
        public virtual ActionResult LogOn(User account, string returnUrl, bool rememberMe)
        {
            if (UserService.ValidateUser(account.UserName, account.Password))
            {
                FormsAuthentication.SetAuthCookie(account.UserName, rememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "ControlPanel");
                }
            }
            else
            {
                ModelState.AddModelError("", "提供的用户名或密码不正确。");
            }

            account.UserName = "test111";
            return View(account);
        }

        public virtual ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
            return Content("LogOn");
            //return RedirectToAction("LogOn");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}